

function birthDeathInit(){
	
}
    
//create children
Node.prototype.spawnChild = function(type, loadObject)
{
	//this is used in window.js, for setting scroll when translating divs 
	window.currentXScroll = window.pageXOffset;
	window.currentYScroll = window.pageYOffset;
	
	var child = newArgument(type, this, loadObject);
	
	this.adoptNode(child);
	return child;
}
	
	
//child management
Node.prototype.adoptNode = function(adoptee)
{
	/*when child is adopted or relinquished:
	 *update relationships
	 *update widths
	 *update locations
 	 *draw connections
	 */
	
	//update relationships/////
	//if assist, add right after assistCreator
	//else
	//add adoptee to end of parent's array of children
	if (adoptee.displayedDiv.classList.contains("assist") )
	{
		this.assists.push(adoptee);
		
		this.updateWidth(adoptee.widthTotal, false);
	}else
	{
		this.children.push(adoptee);
		
		if ( (this.children.length == 1) ) {
		//subtract 220 if first child since 220 default width of childless nodes
			//but only if not adopting an assist
		this.updateWidth(adoptee.widthTotal-220, true);
		}else{
		//else send full width
		this.updateWidth(adoptee.widthTotal, true);
		}	
	}
	
	claim.placeChildren();

	//if X coordinates of new child exceed boundaries of SVG canvas, resize SVG canvas
	var leftShift = adjustXBoundaries(); //function in window.js
	adjustYBoundaries(adoptee.displayedDiv);
	scrollYDiv(adoptee.displayedDiv, leftShift);
	//this must be done before drawing the lines
		//since SVG canvas must be correct before SVG lines are drawn
	
	claim.drawConnections();
	
	//adoptee.displayedDiv.classList.add("transitionReady");
}

Node.prototype.relinquishNode = function(leavingNode)
{
	//"this" is the node losing another node
	//update relationships
	//remove child from parent's array of children
	
	//whether or not to update widthChild or just widthTotal
	var yesChild = leavingNode.displayedDiv.classList.contains("assist")? false:true;
	
	if( !yesChild ){
		
		this.assists.splice(leavingNode.indexToParent, 1);

		//update indexToParent of siblings to its right
		//if it is zero that means the child was the only child, and this does not need to be done
		var i = leavingNode.indexToParent;
		while(i < this.assists.length){ 
			this.assists[i].indexToParent = i;
			i++;
		}
	}else{
		this.children.splice(leavingNode.indexToParent, 1);
		
		//update indexToParent of siblings to its right
		//if it is zero that means the child was the only child, and this does not need to be done
		var i = leavingNode.indexToParent;
		while(i < this.children.length){ 
			this.children[i].indexToParent = i;
			i++;
		}
	}
	
	//update UI
	
	//update width
	//at this point node has already been removed.  That's how length can be 0 when it had a child
		//assists always add width, so doesn't matter if it was last one
	if (this.children.length == 0  && yesChild){
		//subtract 220 if no children left since it started with 220 even when childless
		this.updateWidth(-(leavingNode.widthTotal-220), yesChild );
	}else{
		//else send full width
		this.updateWidth(-leavingNode.widthTotal, yesChild);
	}
	
	claim.placeChildren();
	
	claim.drawConnections();
}

Node.prototype.close = function()
{
    var self = this;
    //this.cancelPendingSave();  //will enable later
    
    //do not allow node to be closed if it has children
    if(this.children[0] ){
    	alert('Cannot delete - has children');
    	return;
    }
    
	//do not allow to be closed if has assists    
    if(this.assists.length != 0){
    	alert('Cannot delete - has assists');
    	return;
    }
    
    //tries to set focus to sibling, then parent, then null
    this.setNewFocus();

	self.cleanUp(self);        
    
	//for deleting from database
	//will enable later
	/*
    db.transaction(function(tx)
    {
        tx.executeSql("DELETE FROM WebKitStickyNotes WHERE id = ?", [self.displayedDiv.id]);
    });
    */
        
}
   
   //run after closing
Node.prototype.cleanUp = function(self){
        
	//let it's parent know to remove it from its list of children
	if(self.parent){
		self.parent.relinquishNode(self);
	}

	//let it's children know to forget it
	//FIX I could also make it so children attach to parent's parent.  Think about what would make most sense
	for (i in self.children){
		self.children[i].parent = null;
	}

	//remove displayedDiv from DOM
	self.DOMParent.removeChild(self.displayedDiv);
	needExpand("close");

	//remove connection label if present
	self.changeLabel("None");
	
	//delete line
	if(self.line){
		self.line.remove();
		self.outline.remove();
	}
	
	if(self.displayedDiv.classList.contains("assist") && self.parent.assists.length == 0)
	{
		self.parent.assistConnection.remove();
	}
}


//updates the indexes to parent of nodes to the right of new assist node
function shiftNextSiblingsIndexToParentRight(newAssistNode){
	
	var i = newAssistNode.indexToParent + 1;
	var parent = newAssistNode.parent;
	var length = parent.children.length;
	while(i < length){ 
		parent.children[i].indexToParent = i;
		i++;
	}
	
}

